package cn.stylefeng.guns.modular.tool.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.AreaId;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionResult;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.tool.entity.ToolTaskArrange;
import cn.stylefeng.guns.modular.tool.entity.ToolWave;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskArrangeParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolTaskArrangeResult;
import cn.stylefeng.guns.modular.tool.service.ToolTaskArrangeService;
import cn.stylefeng.guns.modular.tool.service.ToolWaveService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;


/**
 * 工作安排控制器
 * @author 
 * @Date 2022-11-12 14:03:42
 */
@Controller
@RequestMapping("/toolTaskArrange")
public class ToolTaskArrangeController extends BaseController {

    private String PREFIX = "/tool/toolTaskArrange";

    @Autowired
    private ToolTaskArrangeService toolTaskArrangeService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author 
     * @Date 2022-11-12
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/toolTaskArrange.html";
    }

    /**
     * 新增页面
     *
     * @author 
     * @Date 2022-11-12
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/toolTaskArrange_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 
     * @Date 2022-11-12
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/toolTaskArrange_edit.html";
    }

    @Autowired
    private ToolWaveService toolWaveService;

    @Autowired
    private BasicPositionService basicPositionService;

    /**
     * 新增接口
     *
     * @author 
     * @Date 2022-11-12
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolTaskArrangeParam toolTaskArrangeParam) {
    	ToolTaskArrangeParam check = new ToolTaskArrangeParam();
    	check.setWaveId(toolTaskArrangeParam.getWaveId());
    	check.setGroupNumber(toolTaskArrangeParam.getGroupNumber());
    	check.setGroupUsers(toolTaskArrangeParam.getGroupUsers());
    	ToolTaskArrangeResult checkResult = toolTaskArrangeService.findBySpec(check);
    	if(checkResult != null) {
            return ResponseData.error("已存在");
    	}

//        BasicPositionParam positionParam = new BasicPositionParam();
//        positionParam.setPositionAreaId(AreaId.arrangeAreaId);
//        List<BasicPositionResult> list = basicPositionService.findListBySpec(positionParam, null, null, null, null);
//    	boolean duiying = false;
//    	for (BasicPositionResult pr : list) {
//			if(pr.getPositionNumber().equals(toolTaskArrangeParam.getGroupNumber())) {
//				duiying = true;
//				break;
//			}
//		}
//    	if (!duiying) {
//    		return ResponseData.error(toolTaskArrangeParam.getGroupNumber()+"无对应的拣货位");
//		}
        
    	ToolWave toolWave = toolWaveService.getById(toolTaskArrangeParam.getWaveId());
    	toolTaskArrangeParam.setTaskDate(toolWave.getWaveName());
    	
    	User user = userService.getcurrentUser();
    	toolTaskArrangeParam.setDeptId(user.getDeptId());
    	toolTaskArrangeParam.setDeliverId(user.getDeliverId());
    	toolTaskArrangeParam.setCreateUserName(user.getName());
        this.toolTaskArrangeService.add(toolTaskArrangeParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2022-11-12
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolTaskArrangeParam toolTaskArrangeParam) {
    	ToolTaskArrangeParam check = new ToolTaskArrangeParam();
    	check.setId(toolTaskArrangeParam.getId());
    	check.setWaveId(toolTaskArrangeParam.getWaveId());
    	check.setGroupNumber(toolTaskArrangeParam.getGroupNumber());
    	check.setGroupUsers(toolTaskArrangeParam.getGroupUsers());
    	ToolTaskArrangeResult checkResult = toolTaskArrangeService.findBySpec(check);
    	if(checkResult != null) {
            return ResponseData.error("已存在");
    	}

//        BasicPositionParam positionParam = new BasicPositionParam();
//        positionParam.setPositionAreaId(AreaId.arrangeAreaId);
//        List<BasicPositionResult> list = basicPositionService.findListBySpec(positionParam, null, null, null, null);
//    	boolean duiying = false;
//    	for (BasicPositionResult pr : list) {
//			if(pr.getPositionNumber().equals(toolTaskArrangeParam.getGroupNumber())) {
//				duiying = true;
//				break;
//			}
//		}
//    	if (!duiying) {
//    		return ResponseData.error(toolTaskArrangeParam.getGroupNumber()+"无对应的拣货位");
//		}
    	
    	User user = userService.getcurrentUser();
    	toolTaskArrangeParam.setDeptId(user.getDeptId());
    	toolTaskArrangeParam.setDeliverId(user.getDeliverId());
    	toolTaskArrangeParam.setCreateUserName(user.getName());
        this.toolTaskArrangeService.update(toolTaskArrangeParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 
     * @Date 2022-11-12
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolTaskArrangeParam toolTaskArrangeParam) {
        this.toolTaskArrangeService.delete(toolTaskArrangeParam);
        return ResponseData.success();
    }
    
    /**
     * 批量删除接口
     */
    @RequestMapping("/btnRevoke")
    @ResponseBody
    public ResponseData btnRevoke(Integer[] ids) {
    	for (Integer id : ids) {
    		ToolTaskArrangeParam toolTaskArrangeParam = new ToolTaskArrangeParam();
    		toolTaskArrangeParam.setId(id);
			this.toolTaskArrangeService.delete(toolTaskArrangeParam );
		}
        return ResponseData.success();
    }
    
    /**
     * 清空接口
     */
    @RequestMapping("/btnRevoke72")
    @ResponseBody
    public ResponseData btnRevoke72(ToolTaskArrangeParam toolTaskArrangeParam) {
    	toolTaskArrangeService.deleteBySpec(toolTaskArrangeParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 
     * @Date 2022-11-12
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolTaskArrangeParam toolTaskArrangeParam) {
        ToolTaskArrange detail = this.toolTaskArrangeService.getById(toolTaskArrangeParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 
     * @Date 2022-11-12
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolTaskArrangeParam toolTaskArrangeParam,String condition ) {
    	User user = userService.getcurrentUser();
    	toolTaskArrangeParam.setDeptId(user.getDeptId());
        return this.toolTaskArrangeService.findPageBySpec(toolTaskArrangeParam,condition,user.getOperateId());
    }

}


